<template>
  <div style="display: flex; align-items: center">
    <div class="year-switcher">
      <div @click="handleUp" style="cursor: pointer" class="up-arrow">
        <svg-icon icon-class="arrow-up" />
      </div>
      <div class="container">
        <div
          class="year-item"
          :class="activeList.some((_) => _ === item) ? 'is-active' : ''"
          :style="{ transform: `translateY(${translateY}px)` }"
          v-for="(item, index) in list"
          :key="index"
          @click="handleClick(index)"
        >
          {{ item }}年
        </div>
      </div>
      <div @click="handleDown" style="cursor: pointer" class="down-arrow">
        <svg-icon icon-class="arrow-down" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      required: true,
    },
    activeList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // list: [2015, 2016, 2017, 2018, 2019],
      // activeList: [0, 2, 4],
      translateY: 0,
      move: 0,
    }
  },
  computed: {
    moveCount() {
      return this.list.length - 3
    },
    itemHeight() {
      return document.querySelector('.year-item').offsetHeight
    },
  },
  mounted() {
    // document.querySelector('.down-arrow').firstChild.style.display = 'none';
    if (this.moveCount <= 0) {
      // document.querySelector('.up-arrow').firstChild.style.display = 'none';
      document.querySelector('.container').style.height =
        this.list.length * 37 + 'px'
    }
  },
  updated() {
    // document.querySelector('.down-arrow').firstChild.style.display = 'none';
    if (this.moveCount <= 0) {
      // document.querySelector('.up-arrow').firstChild.style.display = 'none';
      document.querySelector('.container').style.height =
        this.list.length * 37 + 'px'
    }
  },
  watch: {
    move: function (val) {
      console.log(val)
      // if (val === -this.moveCount) {
      //   document.querySelector('.up-arrow').firstChild.style.display = 'none';
      // } else {
      //   document.querySelector('.up-arrow').firstChild.style.display = '';
      // }
      // if (val === 0) {
      //   document.querySelector('.down-arrow').firstChild.style.display = 'none';
      // } else {
      //   document.querySelector('.down-arrow').firstChild.style.display = '';
      // }
    },
  },
  methods: {
    handleUp() {
      if (this.move > -this.moveCount) {
        this.translateY -= this.itemHeight
        this.move -= 1
      }
    },
    handleDown() {
      if (this.move < 0) {
        this.translateY += this.itemHeight
        this.move += 1
      }
    },
    handleClick(index) {
      console.log(index, '------')
      this.$emit('click-year', index)
    },
  },
}
</script>

<style lang="scss" scoped>
.year-switcher {
  width: 75px;
  text-align: center;
  .up-arrow,
  .down-arrow {
    height: 20px;
  }

  .container {
    position: relative;
    overflow-y: hidden;
    height: 111px;
    .year-item {
      text-align: center;
      padding: 8px 4px;
      box-sizing: border-box;
      cursor: pointer;
    }
    .is-active {
      color: #02a7f0;
      background: #e4f8ff;
    }
  }
}
</style>
